// 封装节点类
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// 单向链表结构的封装
class LinkedList {

    constructor() {
        // 初始链表长度为 0
        this.length = 0;

        // 初始 head 为 null，head 指向链表的第一个节点
        this.head = null;
    }
    // toString() 链表数据以字符串形式返回
    toString() {
        let current = this.head, str = ''
        while (current) {
            str += current.data + ' '
            current = current.next
        }
        return str
    }
    // ------------ 链表的常见操作 ------------ //
    append(element) {
        let newNode = new Node(element)
        if (this.length == 0) {
            this.head = newNode
        } else {
            let current = this.head
            while (current.next) {
                current = current.next
            }
            current.next = newNode
        }
        this.length++
    }
    insert(position, element) {
        // 1、对 position 进行越界判断，不能小于 0 或大于链表长度
        if (position < 0 || position > this.length) return false;
        let newNode = new Node(element)
        if (position == 0) {
            newNode.next = this.head
            this.head = newNode
        } else {
            let current = this.head, index = 0, pre = null
            while (index < position) {
                pre = current
                current = current.next
                index++
            }
            pre.next = newNode
            newNode.next = current
        }
        this.length++
    }
    get(position) {
        // 1、position 越界判断
        if (position < 0 || position >= this.length) return null;
        let current = this.head, index = 0
        while (index < position) {
            current = current.next
            index++
        }
        return current.data
    }
    indexOf(element) {
        let current = this.head, index = 0
        while (current && element != current.data) {
            current = current.next
            index++
        }
        if (current == null) return -1
        return index
    }
    update(position, element) {
        let current = this.head, index = 0
        while (index < position) {
            current = current.next
            index++
        }
        current.data = element
        return current
    }
}
console.log('// ----- 单向链表结构测试 START -----//');
const linkedList = new LinkedList();

// 测试 append 方法
linkedList.append('AA');
linkedList.append('BB');
linkedList.append('CC');
console.log(linkedList.toString());

// 测试 insert 方法
linkedList.insert(0, '123');
linkedList.insert(2, '456');
console.log(linkedList.toString()); //--> 123 AA 456 BB CC

// 测试 getData 方法
console.log(linkedList.get(0)); //--> 123
console.log(linkedList.get(1)); //--> AA
console.log(linkedList.get(10)); //--> null

// 测试 indexOf 方法
console.log(linkedList.indexOf('AA')); //--> 1
console.log(linkedList.indexOf('ABC')); //--> -1

// 测试 update 方法
linkedList.update(0, '12345');
console.log(linkedList.toString()); //--> 12345 AA 456 BB CC
linkedList.update(1, '54321');
console.log(linkedList.toString()); //--> 12345 54321 456 BB CC